package org.dromara.nav.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.nav.domain.vo.OsProjectVo;
import org.dromara.nav.domain.bo.OsProjectBo;
import org.dromara.nav.service.IOsProjectService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 开源项目
 *
 * @author LSY
 * @date 2025-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/nav/project")
public class OsProjectController extends BaseController {

    private final IOsProjectService osProjectService;

    /**
     * 查询开源项目列表
     */
    @SaCheckPermission("nav:project:list")
    @GetMapping("/list")
    public TableDataInfo<OsProjectVo> list(OsProjectBo bo, PageQuery pageQuery) {
        return osProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出开源项目列表
     */
    @SaCheckPermission("nav:project:export")
    @Log(title = "开源项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OsProjectBo bo, HttpServletResponse response) {
        List<OsProjectVo> list = osProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "开源项目", OsProjectVo.class, response);
    }

    /**
     * 获取开源项目详细信息
     *
     * @param projectId 主键
     */
    @SaCheckPermission("nav:project:query")
    @GetMapping("/{projectId}")
    public R<OsProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long projectId) {
        return R.ok(osProjectService.queryById(projectId));
    }

    /**
     * 新增开源项目
     */
    @SaCheckPermission("nav:project:add")
    @Log(title = "开源项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OsProjectBo bo) {
        return toAjax(osProjectService.insertByBo(bo));
    }

    /**
     * 修改开源项目
     */
    @SaCheckPermission("nav:project:edit")
    @Log(title = "开源项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OsProjectBo bo) {
        return toAjax(osProjectService.updateByBo(bo));
    }

    /**
     * 删除开源项目
     *
     * @param projectIds 主键串
     */
    @SaCheckPermission("nav:project:remove")
    @Log(title = "开源项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] projectIds) {
        return toAjax(osProjectService.deleteWithValidByIds(List.of(projectIds), true));
    }
}
